
import plugin from '../../../../lib/plugins/plugin.js';
import RobotError from '../model/errors/robot-error.js';
import GuessHand from '../service/guess_hand.js';
import REDIS from '../util/redis.js';
import { getPlayerByE, pm } from './main.js';


export class CaiQuan extends plugin {
    constructor() {
        super({
            name: 'LNE',
            dsc: '机器人',
            event: 'message',
            priority: 500,
            rule: [
                {
                    /** 命令正则匹配 */
                    reg: '^猜拳[0-9]*$',
                    /** 执行方法 */
                    fnc: 'caiquanHelp'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^猜拳(剪刀|石头|布)? [0-9]+$',
                    /** 执行方法 */
                    fnc: 'caiquan'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^猜数字$',
                    /** 执行方法 */
                    fnc: 'guessNumHelp'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^猜数字( )?[0-9]+$',
                    /** 执行方法 */
                    fnc: 'guessNum'
                },
            ]
        });
    }

    async caiquanHelp() {
        super.reply("命令：猜拳(石头|剪刀|布) [数字]");
    }

    async caiquan(e) {
        let betting = Number.parseInt(e.msg.split(' ')[1]);
        let player = await getPlayerByE(e);
        let gh = new GuessHand(player);

        let hand = gh.strToHand(e.msg.split(' ')[0].replace("猜拳", ""));
        try {
            let { msg } = gh.do(hand, betting);
            await pm.savePlayer(player);
            await super.reply(msg);
        } catch (err) {
            await super.reply(err.message);
        }
    }

    async guessNum(e) {
        let player = await getPlayerByE(e);
        if (player.coin < 100) {
            await super.reply("硬币不足");
            return;
        }

        let jackpot = Number.parseInt(await REDIS.get(`LNE:game:guess_num:jackpot`));
        let correctNum: number;
        if (Number.isNaN(jackpot) || jackpot == 0) {
            jackpot = 1000;
            correctNum = Math.floor(Math.random() * 100);
            await REDIS.set(`LNE:game:guess_num:correct_num`, correctNum);
            await REDIS.set(`LNE:game:guess_num:jackpot`, jackpot);
        } else correctNum = Number.parseInt(await REDIS.get(`LNE:game:guess_num:correct_num`));

        let msg: string = e.msg;
        let playerNum = Number.parseInt(msg.replace("猜数字", "").replace(" ", ""));
        if (playerNum == correctNum) {
            player.coin += jackpot;
            await REDIS.set(`LNE:game:guess_num:jackpot`, 0);
            await super.reply(`猜对了！你获得了奖池内所有的硬币！\n你现在有${player.coin}硬币`);
        } else {
            player.coin -= 100;
            jackpot += 100;
            await REDIS.set(`LNE:game:guess_num:jackpot`, jackpot);
            await super.reply(`猜错了，现在奖池内有${jackpot}硬币\n你现在还有${player.coin}硬币`);
        }
        await pm.savePlayer(player);

    }

    async guessNumHelp() {
        super.reply(`猜数字\n开始游戏时奖池内将会有1000的初始硬币\n并会随机选择一个0~99的随机数\n` +
            `每轮玩家需要猜数字\n若猜对，奖池内所有硬币归你所有\n若猜错，每次付出100硬币投入奖池。`);
    }

}

